/*******************************************************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Nicolas DAURES
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *******************************************************************************************************************/


/****************************************************************
 * Time classes
 ****************************************************************/

// Compute the deltatime since last iteration
function TimeEngine()
{
	//=======================
	// Fields
	//=======================

	this.m_fTime =		(new Date()).getTime();
	this.m_fTotalTime =	0;
	this.m_fDeltaTime =	0;

	this.m_iFps =		60;
	this.m_FpsText =	new Text2D();
}


//=======================
// Accessors
//=======================

TimeEngine.prototype.getCurrentTime = function()
{
    return this.m_fTime;
}

TimeEngine.prototype.getTotalTime = function()
{
    return this.m_fTotalTime;
}

TimeEngine.prototype.getDeltaTime = function()
{
    return this.m_fDeltaTime;
}

TimeEngine.prototype.getFPS = function()
{
    return this.m_iFps;
}


//=======================
// Operations
//=======================

TimeEngine.prototype.update = function()
{
    var fNewTime = (new Date()).getTime();
    this.m_fDeltaTime = (fNewTime - this.m_fTime) * 0.001;
    this.m_fTime = fNewTime;
    this.m_fTotalTime += this.m_fDeltaTime;

    // TODO : save last 10 fps
    var iFPS = Math.floor(1 / this.m_fDeltaTime);
    this.m_iFps = Math.floor((this.m_iFps * 9 + iFPS) * 0.1);
    this.m_FpsText.m_Text = "FPS : " + this.m_iFps;
    if (this.m_iFps > 50)
    {
        this.m_FpsText.m_Color = "#00FF00";
    }
    else if (this.m_iFps > 30)
    {
        this.m_FpsText.m_Color = "#FFAA00";
    }
    else
    {
        this.m_FpsText.m_Color = "#FF0000";
    }
}

TimeEngine.prototype.displayFPS = function(a_v2Position)
{
    this.m_FpsText.m_Font = "italic small-caps bold 16px arial";
    this.m_FpsText.getSceneNode().m_v2Pos.x = a_v2Position.x;
    this.m_FpsText.getSceneNode().m_v2Pos.y = a_v2Position.y;
    this.m_FpsText.setPriority(-1);
    CameraEngine.getInstance().m_SceneNode.attachSceneNode(this.m_FpsText.m_SceneNode);
    RenderEngine.getInstance().addRenderable(this.m_FpsText);
}

TimeEngine.m_Instance = null;
TimeEngine.getInstance = function()
{  
    if (this.m_Instance == null)
    {  
		this.m_Instance = new TimeEngine();  
    }  

    return this.m_Instance;  
}
